from flask import Flask,jsonify,request,Blueprint
from user_agents import parse


useTest = Blueprint('useTest',__name__)

@useTest.route("/usetest/getdevice",methods=['POST','GET'])
def getdevice():
    user_agent_string = request.headers.get('User-Agent')
    user_agent = parse(user_agent_string)

    device = user_agent.device.family
    browser = user_agent.browser.family
    version = user_agent.browser.version_string
    os = user_agent.os.family
    is_bot = user_agent.is_bot
    is_mobile = user_agent.is_mobile
    return (f"Device: {device}, Browser: {browser}, Version: {version}, OS: {os}, "
            f"Bot: {is_bot}, Mobile: {is_mobile}")


@useTest.route("/usetest/getip",methods=['POST','GET'])
def getip():
    # 尝试获取原始IP地址
    xff = request.headers.get('X-Forwarded-For')
    # print(xff)
    if xff:
        # 通常第一个IP地址是客户端的IP地址
        client_ip = xff.split(',')[0]
    else:
        client_ip = request.remote_addr
    return f"Your IP address is: {client_ip}"











